package com.hurui.xiaoming.adapter;

import android.Manifest;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.hurui.xiaoming.R;
import com.hurui.xiaoming.bean.ManagerBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/8/25.
 */

public class ManagerAdapter extends RecyclerView.Adapter<ManagerAdapter.Holder> {
    private  ClipboardManager cmb;
    Context context;
    List<ManagerBean.DataBean> lists;

    public ManagerAdapter(Context context) {
        this.context = context;
        lists = new ArrayList<>();
        cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    }

    public void refresh(List<ManagerBean.DataBean> list) {
        this.lists.addAll(list);
        Log.e("wlx", "refresh: " + lists.size());
        notifyDataSetChanged();
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.manager_tem, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(final Holder holder, int position) {
        final ManagerBean.DataBean dataBean = lists.get(position);
        holder.mItem_weixin.setText(dataBean.getUi_qqwx());
        holder.mItem_phone.setText(dataBean.getUi_phone());
        holder.name.setText(dataBean.getUi_name());
        holder.mItem_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataBean.getUi_phone() != null) {
                    cmb.setText(dataBean.getUi_phone());
                    Toast.makeText(context, "已复制到剪切板", Toast.LENGTH_SHORT).show();

                }
            }
        });
        holder.mItem_weixin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (dataBean.getUi_qqwx() != null) {
                    cmb.setText(dataBean.getUi_qqwx());
                    Toast.makeText(context, "已复制到剪切板", Toast.LENGTH_SHORT).show();

                }
            }
        });
        holder.mItem_dialog_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                order_phone(dataBean.getUi_phone());
            }
        });
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemCount() {
        return lists != null ? lists.size() : 0;
    }

    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.CALL_PHONE};
    private void order_phone(String phone) {

        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            //申请WRITE_EXTERNAL_STORAGE权限
            Toast.makeText(context,"你为开通拨打电话的权限，请前往开通...",Toast.LENGTH_SHORT).show();

            return;
        }else{
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
            context.startActivity(intent);
        }


//        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
//            return;
//        }
    }

    class  Holder extends RecyclerView.ViewHolder {
          TextView name;
        TextView mItem_weixin;
         TextView mItem_phone;
         TextView mItem_dialog_phone;

        public Holder(View itemView) {
            super(itemView);
           mItem_weixin= ((TextView) itemView.findViewById(R.id.item_weixin));
           mItem_phone= ((TextView) itemView.findViewById(R.id.item_phone));
            mItem_dialog_phone=((TextView)itemView.findViewById(R.id.item_dialog_phone));
            name = ((TextView) itemView.findViewById(R.id.item_name));
        }
    }


}
